package com.kikt.custombtndemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;

import com.kikt.custombtndemo.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ValueAnimator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by kikt on 2016/5/7.
 */
public class SignButton extends FrameLayout implements View.OnClickListener {
    private final Context mContext;
    protected LayoutInflater mInflater;
    @Bind(R.id.ib_center)
    ImageButton mIbCenter;
    @Bind(R.id.fl_container)
    FrameLayout mFlContainer;

    protected int start = 0;
    protected int end = 500;
    protected int num = 100;
    protected int delay = 500;
    protected int duration = 1000;

    public SignButton(Context context) {
        this(context, null);
    }

    public SignButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SignButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mInflater.inflate(R.layout.button_sign, this, true);
        ButterKnife.bind(this);
        init(attrs);
        initView();
    }

    List<CircleView> circleViewList = new ArrayList<>();

    private void initView() {
        mIbCenter.setOnClickListener(this);

        for (int i = 0; i < num; i++) {
            CircleView circleView = new CircleView(mContext);
            FrameLayout.LayoutParams params = new LayoutParams(10, 10);
            params.gravity = Gravity.CENTER;
            circleView.setVisibility(INVISIBLE);
            circleView.setLayoutParams(params);
            circleView.setColor(colors[i % colors.length]);
            mFlContainer.addView(circleView);
            circleViewList.add(circleView);
        }
    }

    private void init(AttributeSet attrs) {
    }

    private void start() {
        for (CircleView circleView : circleViewList) {
            circleView.setVisibility(INVISIBLE);
        }

        for (int i = 0; i < circleViewList.size(); i++) {
            CircleView circleView = circleViewList.get(i);
            start(circleView, i);
        }
    }

    public int[] colors = new int[]{Color.RED, Color.BLUE, Color.GREEN};

    private void start(final CircleView circleView, final int index) {
        final int delay = index * this.delay/3;
        ValueAnimator animator = ValueAnimator.ofInt(start, end).setDuration(this.duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = circleView.getLayoutParams();
                layoutParams.height = animatedValue;
                circleView.getLayoutParams().width = animatedValue;
                circleView.requestLayout();
                float animatedFraction = animation.getAnimatedFraction();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                circleView.setVisibility(VISIBLE);
            }
        });
        animator.setStartDelay(delay);
        animator.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_center:
                start();
                break;
        }
    }

    public class CircleView extends View {
        public static final int COLOR = Color.RED;
        protected Paint paint;
        private int color = COLOR;
        protected PaintFlagsDrawFilter paintFlagsDrawFilter;

        public CircleView(Context context) {
            super(context);
            paint = new Paint();
            paintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        }

        public void setColor(int color) {
            this.color = color;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            paint.setColor(color);
            int width = getWidth();
            int height = getHeight();
            canvas.setDrawFilter(paintFlagsDrawFilter);
            canvas.drawCircle(width / 2, height / 2, getRadius(), paint);
        }

        private float getRadius() {
            return getWidth() / 2;
        }

    }
}
